package com.icss.ah.purchaseGoods.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.Json;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.ah.purchaseGoods.service.PurchaseGoodsService;

@Controller
public class PurchaseGoodsAction extends BaseAction {

	private static final long serialVersionUID = 1208922087585083101L;
	
	@Autowired
	private PurchaseGoodsService purchaseGoodsService;
	
	public String toPurchaseGoodsPage(){

		UserInfoBean userInfo=getUserInfo("");
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		Map<String, String> map  = new HashMap<String, String>();
		map.put("shopId", shopId);
		map.put("corpId", corpId);
		List<Map<String, Object>> yjfl =  purchaseGoodsService.getProductTypeList(map);
		
		request = this.getRequest();
		request.setAttribute("yjfl", yjfl);
		return "toPurchaseGoodsPage";
	}
	
	public void queryPurchaseGoodsList(){
		request = this.getRequest();
		
		UserInfoBean userInfo=getUserInfo("");
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		
		String order  = request.getParameter("order");
		if(order==null||"".equals(order)){
			order = " DOWN_TIME DESC ";
		}
		String title  = request.getParameter("title").trim();
		if(title==null||"".equals(title)){
			title = "";
		}
		String minPriceStr  = request.getParameter("minPriceStr")==null?"":request.getParameter("minPriceStr").trim();
		String maxPriceStr  = request.getParameter("maxPriceStr")==null?"":request.getParameter("maxPriceStr").trim();
		
		String firstSeries  = request.getParameter("firstSeries")==null?"0":request.getParameter("firstSeries").trim();
		String secondSeries  = request.getParameter("secondSeries")==null?"0":request.getParameter("secondSeries").trim();
		String serise ="";
		if(secondSeries==null||"".equals(secondSeries)||"0".equals(secondSeries)||"null".equals(secondSeries)){
			if(firstSeries!=null&&!"null".equals(firstSeries)&&!"0".equals(firstSeries)) 
			serise = firstSeries==null?"":firstSeries;
		}else{
			 serise = secondSeries;
		}
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("corpId",corpId);	//公众号id
		map.put("shopId", shopId);	//商铺id
		map.put("order", order);
		map.put("title", title);
		map.put("minPriceStr", minPriceStr);
		map.put("maxPriceStr", maxPriceStr);
		map.put("serise",serise);
		map.put("startNo",getStartIndexMysql());
		map.put("pageRows",getEndIndexMysql());
		dataGridJson = purchaseGoodsService.queryPurchaseGoodsList(map);
		str2Resp(JSONObject.fromObject(dataGridJson).toString());
	}
	
	/**
	 * 商品上架
	 */
	public void productUp(){
		jsonText = new Json();
		request = this.getRequest();
		UserInfoBean userInfo=getUserInfo("");
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		String productIds = request.getParameter("productIds");
		String[] ids = productIds.split(",");
		for(int i=0;i<ids.length;i++){
			if(!"".equals(ids[i])){
				Map<String, String> map  = new HashMap<String, String>();
				map.put("productId", ids[i]);
				map.put("corpId", corpId);
				map.put("shopId", shopId);
				purchaseGoodsService.productUp(map);
			}
		}
		jsonText.setSuccess(true);
		str2Resp(JSONObject.fromObject(jsonText).toString());
	}
	
	/**
	 * 删除未上架商品
	 */
	public void productDel(){
		jsonText = new Json();
		request = this.getRequest();

		UserInfoBean userInfo=getUserInfo("");
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		String productIds = request.getParameter("productIds");
		String[] ids = productIds.split(",");
		for(int i=0;i<ids.length;i++){
			if(!"".equals(ids[i])){
				Map<String, String> map  = new HashMap<String, String>();
				map.put("productId", ids[i]);
				map.put("corpId", corpId);
				map.put("shopId", shopId);
				purchaseGoodsService.productDel(map);
			}
		}
		jsonText.setSuccess(true);
		str2Resp(JSONObject.fromObject(jsonText).toString());
	}
	
	/**
	 * 购进商品挑选页
	 * @return
	 */
	public String toSearchProduct(){
		return "toSearchProductPage";
	}
	
	private String productName; //商品名称
	
	/**
	 * 商品列表页查询
	 */
	public void queryProductInfoList(){
		UserInfoBean user = getUserInfo("");
		Map<String, String> map = new HashMap<String, String>();
		map.put("licenceNo", user.getLicenceNo());
		map.put("productName", productName);
		map.put("startIndex", getStartIndexMysql());
		map.put("rowNums", getEndIndexMysql());
		dataGridJson = purchaseGoodsService.queryProductInfoList(map);
		str2Resp(JSONObject.fromObject(dataGridJson).toString());
	}
	/**
	 * 保存挑选商品
	 */
	public void saveSearchProductInfo(){
		request = getRequest();
		jsonText = new Json();
		String uuids = request.getParameter("uuids");
		uuids = uuids == null ? "" : uuids;
		if(!uuids.equals("")){
			UserInfoBean user = getUserInfo("");
			Map<String, String> map = new HashMap<String, String>();
			map.put("corpId", user.getCorpId());
			map.put("shopId", user.getShopId());
			map.put("licenceNo", user.getLicenceNo());
			map.put("uuids", uuids);
			String result = purchaseGoodsService.saveSearchProductInfo(map);
			String[] str = result.split(",");
			if(str[0].equals("1")){
				jsonText.setSuccess(true);
			} else {
				jsonText.setSuccess(false);
			}
			jsonText.setMsg(str[1]);
		} else {
			jsonText.setSuccess(false);
			jsonText.setMsg("未选择商品");
		}
		str2Resp(JSONObject.fromObject(jsonText).toString());
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

}
